//
//  SQStatusListViewModel.swift
//  SwiftWeibo
//
//  Created by suqianghotel on 16/11/29.
//  Copyright © 2016年 suqianghotel. All rights reserved.
//

import Foundation
import YYModel



class SQStatusListViewModel {
    
    lazy var statusViewModels = [SQStatusViewModel]()
    
    
    func loadStatuses(isPullup:Bool, completion: @escaping (_ isSuccess: Bool) -> ())
    {
        
        let since_id = isPullup == true ? 0 : self.statusViewModels.first?.status.id ?? 0
        
        let max_id = isPullup == false ? 0 : self.statusViewModels.last?.status.id ?? 0
        
        SQHttpManager
            .shared
            .status_home_timeline(
                since_id: since_id,
                max_id: max_id,
                success:{list in
                guard
                    
                    let statuses = NSArray.yy_modelArray(with: SQStatus.self, json: list ?? []) as? [SQStatus]
                    
                else {
                    
                    completion(false)
                    
                    return

                }
                    
                    
                var statusViewModels:[SQStatusViewModel] = [SQStatusViewModel]()
                    
                for dict in list ?? []
                {
            
                    let status = SQStatus.yy_model(with: dict)
                    
                    if status != nil
                    {
                        let vm =  SQStatusViewModel(status: status!)
                        statusViewModels.append(vm)

                    }
                    
                }
                    
                                                    
                                                    
                                                    
                if(isPullup)
                {
                    print("上拉：一共加载了\(statuses.count)条数据")
                    
                    if statuses.count == 0
                    {
                        print("上拉没有更多数据了~");
                    }
                    self.statusViewModels += statusViewModels
                }
                else
                {
                    print("下拉：一共加载了\(statuses.count)条数据")
                    
                    self.statusViewModels = statusViewModels + self.statusViewModels
                }
    
                    
                self.statusViewModels += self.statusViewModels
                self.statusViewModels += self.statusViewModels

                
                completion(true)

        },
                failure: { (msg, code) in

        })
        

    }
    
}
